home *** CD-ROM | disk | FTP | other *** search
/ Experimental BBS Explossion 3 / Experimental BBS Explossion III.iso / msdos / 4utils80.zip / HANDLEIN.PAS < prev    next >
Pascal/Delphi Source File  |  1993-12-17  |  6KB  |  227 lines

  1. UNIT HandleIniFile;
  2. (* ----------------------------------------------------------------------
  3.    Part of 4DESC - A Simple 4DOS File Description Editor
  4.        and 4FF   - 4DOS File Finder
  5.  
  6.    (c) 1992, 1993 Copyright by
  7.  
  8.        David Frey,         & Tom Bowden
  9.        Urdorferstrasse 30    1575 Canberra Drive
  10.        8952 Schlieren ZH     Stone Mountain, GA 30088-3629
  11.        Switzerland           USA
  12.  
  13.        Code created using Turbo Pascal 7.0 (c) Borland International 1990
  14.  
  15.    DISCLAIMER: This unit is freeware: you are allowed to use, copy
  16.                and change it free of charge, but you may not sell or hire
  17.                this part of 4DESC. The copyright remains in our hands.
  18.  
  19.                If you make any (considerable) changes to the source code,
  20.                please let us know. (send a copy or a listing).
  21.                We would like to see what you have done.
  22.  
  23.                We, David Frey and Tom Bowden, the authors, provide absolutely
  24.                no warranty of any kind. The user of this software takes the
  25.                entire risk of damages, failures, data losses or other
  26.                incidents.
  27.  
  28.    This unit handles the reading of settings stored in 4UTILS.INI.
  29.  
  30.    ----------------------------------------------------------------------- *)
  31.  
  32. INTERFACE
  33.  
  34. CONST INIFileExists : BOOLEAN = FALSE;
  35.  
  36. PROCEDURE ReadIniFile;
  37.  
  38. FUNCTION  ReadSettingsChar(Section,Name: STRING; Default: CHAR): CHAR;
  39. FUNCTION  ReadSettingsString(Section,Name: STRING; Default: STRING): STRING;
  40. FUNCTION  ReadSettingsInt(Section,Name: STRING; Default: INTEGER): INTEGER;
  41. FUNCTION  ReadSettingsColor(Section,Name: STRING; Default: INTEGER): BYTE;
  42.  
  43. IMPLEMENTATION USES Dos, Crt, StringDateHandling;
  44.  
  45. CONST INIFileName = '4UTILS.INI';
  46.       MaxLines    = 41;
  47.  
  48. VAR INIFile : TEXT;
  49.     INIPath : DirStr;
  50.     MaxLine : INTEGER;
  51.     Line    : ARRAY[1..MaxLines] OF STRING[48];
  52.  
  53.     Name    : NameStr;
  54.     Ext     : ExtStr;
  55.     IORes   : INTEGER;
  56.  
  57. PROCEDURE ReadIniFile;
  58.  
  59. VAR semicol : BYTE;
  60.     ReadLine: STRING;
  61.     s       : STRING;
  62.  
  63.  PROCEDURE SearchPath;
  64.  (* Search for INIFile in the PATH *)
  65.  
  66.   BEGIN
  67.    INIPath := FSearch(INIFileName,GetEnv('PATH'));
  68.    IF INIPath > '' THEN
  69.      BEGIN
  70.        {$I-}
  71.        Assign(INIFile,INIPath);
  72.        Reset(INIFile);
  73.        INIFileExists := (IOResult = 0);
  74.        {$I+}
  75.      END;
  76.   END; (* SearchPath *)
  77.  
  78. BEGIN  (* ReadIniFile *)
  79.  (* Search strategy for 4UTILS.INI :
  80.       i) Directory where this application has started from.
  81.      ii) Environment variable 4UTILS
  82.     iii) Path                                                   *)
  83.  
  84.  FSplit(ParamStr(0),INIPath,Name,Ext);
  85.  {$I-}
  86.  Assign(INIFile,INIPath+INIFileName);
  87.  Reset(INIFile);
  88.  {$I+}
  89.  INIFileExists := (IOResult = 0);
  90.  IF NOT INIFileExists THEN
  91.   BEGIN
  92.    INIPath := GetEnv('4UTILS');
  93.    IF INIPath > '' THEN
  94.      BEGIN
  95.        IF INIPath[Length(INIPath)] <> '\' THEN INIPath := INIPath + '\';
  96.        {$I-}
  97.        Assign(INIFile,INIPath+INIFileName);
  98.        Reset(INIFile);
  99.        {$I+}
  100.        INIFileExists := (IOResult = 0);
  101.        IF NOT INIFileExists THEN SearchPath;
  102.      END
  103.    ELSE SearchPath;
  104.   END;
  105.  
  106.  IF INIFileExists THEN
  107.   BEGIN
  108.    MaxLine := 1;
  109.    WHILE NOT Eof(INIFile) AND (MaxLine <= MaxLines) DO
  110.     BEGIN
  111.      ReadLn(INIFile,ReadLine);
  112.      StripLeadingSpaces(ReadLine);
  113.  
  114.      IF (ReadLine[1] <> ';') THEN
  115.       BEGIN
  116.        semicol := Pos(';',ReadLine);
  117.        IF (semicol > 0) AND
  118.           (DownStr(Copy(ReadLine,1,10)) <> 'delimiters') THEN
  119.         ReadLine := Copy(ReadLine,1,semicol-1);
  120.  
  121.        IF Length(ReadLine) > 0 THEN
  122.         BEGIN
  123.          StripTrailingSpaces(ReadLine);
  124.          Line[MaxLine] := DownStr(ReadLine); INC(MaxLine);
  125.         END;
  126.       END; (* IF Line[1] <> ';' .. *)
  127.     END; (* WHILE *)
  128.  
  129.    IF NOT Eof(INIFile) THEN
  130.     BEGIN
  131.      WriteLn('WARNING: ',INIFileName,' exceeds ',MaxLines,' lines!');
  132.      WriteLn('         Parts of your settings will be ignored.');
  133.      Delay(5000);
  134.     END;
  135.  
  136.    {$I-}
  137.    Close(INIFile); IORes := IOResult;
  138.    {$I+}
  139.   END; (* INIFileExists .. *)
  140. END; (* ReadIniFile *)
  141.  
  142. (* A collection of functions to read Strings/Values/Color names out of
  143.    a initialisation file.                                              *)
  144.  
  145. FUNCTION  ReadSettingsString(Section,Name: STRING; Default: STRING): STRING;
  146.  
  147. VAR LineNr: BYTE;
  148.     eq    : BYTE;
  149.     s,res : STRING;
  150.  
  151. BEGIN
  152.  LineNr := 1;
  153.  Section := '['+DownStr(Section)+']';
  154.  WHILE (Line[LineNr] <> Section) AND (LineNr <= MaxLine) DO INC(LineNr);
  155.  IF Line[LineNr] = Section THEN
  156.   BEGIN
  157.    DownString(Name); res := ''; s := '';
  158.    REPEAT
  159.     s  := Line[LineNr]; eq := Pos('=',s);
  160.     IF eq > 0 THEN s := Copy(s,1,eq-1);
  161.     StripTrailingSpaces(s);
  162.     INC(LineNr);
  163.    UNTIL (s = Name) OR (LineNr > MaxLine);
  164.  
  165.    IF s = Name THEN
  166.     BEGIN
  167.      res := Copy(Line[LineNr-1],eq+1,255);
  168.      StripLeadingSpaces(res);
  169.     END
  170.    ELSE res := Default;
  171.   END
  172.  ELSE res:= Default;
  173.  ReadSettingsString := res;
  174. END;
  175.  
  176. FUNCTION  ReadSettingsChar(Section,Name: STRING; Default: CHAR): CHAR;
  177.  
  178. VAR s : STRING;
  179.  
  180. BEGIN
  181.  s := ReadSettingsString(Section,Name,Default);
  182.  ReadSettingsChar := s[1];
  183. END;
  184.  
  185. FUNCTION  ReadSettingsInt(Section,Name: STRING; Default: INTEGER): INTEGER;
  186.  
  187. VAR s  : STRING;
  188.     res: INTEGER;
  189.     v  : INTEGER;
  190.  
  191. BEGIN
  192.  Str(Default,s);
  193.  s := ReadSettingsString(Section,Name,s);
  194.  Val(s,v,res);
  195.  IF res > 0 THEN v := Default;
  196.  ReadSettingsInt := v;
  197. END;
  198.  
  199. FUNCTION  ReadSettingsColor(Section,Name: STRING; Default: INTEGER): BYTE;
  200.  
  201. CONST color : ARRAY[0..15] OF STRING[12] =
  202.               ('black'   ,'blue'        ,'green'     ,'cyan'     ,
  203.                'red'     ,'magenta'     ,'brown'     ,'lightgray',
  204.                'darkgray','lightblue'   ,'lightgreen','lightcyan',
  205.                'lightred','lightmagenta','yellow'    ,'white');
  206.  
  207. VAR s  : STRING;
  208.     c  : BYTE;
  209.  
  210. BEGIN
  211.  Str(Default,s);
  212.  s := ReadSettingsString(Section,Name,'');
  213.  
  214.  IF s > '' THEN
  215.   BEGIN
  216.    c := 0;
  217.    WHILE (color[c] <> s) AND (c<16) DO INC(c);
  218.    IF color[c] <> s THEN c := Default;
  219.   END
  220.  ELSE c := Default;
  221.  ReadSettingsColor := c;
  222. END;
  223.  
  224. BEGIN
  225.  ReadIniFile;
  226. END.
  227.